﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Windows.Forms;
using System.Collections;
using AgentTest;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using GeneralDFS.Common.Network;
using GeneralDFS.DataEntity.Config;
using GeneralDFS.Client;
using System.Data.SqlClient;
using GeneralDFS.Interface.Agent;
using NIF.MobileActors.Core;
using NIF.MobileActors.Hosts;

namespace DXWinFormDataAnalysis
{
    public partial class Form1 : XtraForm
    {
        private Client _client;
        private DFSConfig _config;
        private DataTable _dataTable;
        private WaitForm1 wform;

        public Form1()
        {
            InitializeComponent();
            this.Deactivate+=Form1_Deactivate;
            this.Activated += Form1_Activated;
            _client=new Client();
            _config=_client.GetConfig();
            _dataTable=new DataTable();
        }

        void Form1_Activated(object sender, EventArgs e)
        {
            if (wform != null)
            {
                wform.WindowState = FormWindowState.Normal;
                wform.Visible = true;
            }
        }

        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitPivotGrid();
          
        }
 
        void InitPivotGrid()
        {
            // Change this property to transpose the chart.
            pivotGridControl.OptionsChartDataSource.ProvideDataByColumns = false;

            pivotGridControl.BeginUpdate();
            pivotGridControl.DataSource = _config.DataNodeConfigs;
            pivotGridControl.EndUpdate();
        
        }
   
        void InitDataTable()
        {
            _dataTable.Columns.Add("ServerName",typeof(string));


        }

        private void bt_SearchNodesData_Click(object sender, EventArgs e)
        {
            wform = new WaitForm1();
      //   wform.Location.Offset(Screen.FromControl(this).WorkingArea.Width, Screen.FromControl(this).WorkingArea.Height);
            wform.StartPosition=FormStartPosition.CenterScreen;
            wform.Show();
            RemotingHost.RegisterLocal(IPHelper.GetLocalIP("network"), 8820);

            ActorProxy<IEntry> localActor = RemotingHost.LocalFacade.CreateLocalActor<IEntry>(typeof(SearchNodesActor));
            SearchNodesActor act = localActor.GetActor() as SearchNodesActor;
            Client client = new Client();
            Queue<IPEndPoint> hosts = new Queue<IPEndPoint>();
            foreach (NameNodeConfig nameNodeConfig in client.GetConfig().NameNodeConfigs)
            {
                var endPoint = new IPEndPoint(IPAddress.Parse(nameNodeConfig.Ip), nameNodeConfig.AgentRpcPort);
                hosts.Enqueue(endPoint);
            }

            act.Hosts = hosts;
            act.LocalHost = new IPEndPoint(IPAddress.Parse(IPHelper.GetLocalIP("network")), 8820);
            try
            {

            localActor.Post(h => h.Excute());
            ActorProxy<IEntry> actor = RemotingHost.LocalFacade.GetActorProxy<IEntry>(localActor.ID);
            while (((SearchNodesActor)actor.GetActor()).IsLastHost)
            {
                MessageBox.Show("hello");
            }

            }
            catch (Exception exception)
            {
                throw exception;
            }
            /*
            ActorProxy<IHello> proxy = RemotingHost.LocalFacade.GetActorProxy<IHello>(localActor.ID);
            if (proxy != null)
            {
                proxy.Post(h => h.Excute());
            }
          
            
            Console.ReadKey();  */
       
            wform.Load+=wform_Load;
        }

        private void wform_Load(object sender, EventArgs e)
        {
            
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                if (wform != null && wform.WindowState == FormWindowState.Normal)
                {
                    wform.WindowState=FormWindowState.Minimized;
                    wform.Visible = false;
                }
            }
            else if(this.WindowState==FormWindowState.Normal)
            {
                if (wform != null && wform.WindowState == FormWindowState.Minimized)
                {
                    wform.WindowState=FormWindowState.Normal;
                    wform.Visible = true;
                }
            }
        }
         
    }
}
